package com.example.wechatapp;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.wechatapp.fragment.AddressBookFragment;
import com.example.wechatapp.fragment.DiscoveryFragment;
import com.example.wechatapp.fragment.HomeFragment;
import com.example.wechatapp.fragment.MeFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity2 extends AppCompatActivity {
    // 创建一个Fragment列表
    List<Fragment> list;

    private BottomNavigationView bottomNavigationView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        System.out.println("dev分支");



        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        // 初始化list,并将Fragment添加到list中
        list = new ArrayList<>();
        list.add(new HomeFragment());
        list.add(new DiscoveryFragment());
        list.add(new AddressBookFragment());
        list.add(new MeFragment());

        // 默认显示第一个Fragment,即首页
        showFragment(list.get(0));

        // 初始化底部导航栏
        bottomNavigationView = findViewById(R.id.bottomNavigation);
        // 设置底部导航栏的点击监听器
        bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.menu_home:
                        showFragment(list.get(0));
                        break;
                    case R.id.menu_address_book:
                        showFragment(list.get(1));
                        break;
                    case R.id.menu_discovery:
                        showFragment(list.get(2));
                        break;
                    case R.id.menu_me:
                        showFragment(list.get(3));
                        break;
                }
                return true;
            }
        });

        // 默认选中第一个菜单项
//        bottomNavigationView.setSelectedItemId(R.id.menu_item1);
    }

    // 显示指定Fragment
    private void showFragment(Fragment fragment){
        // 获取 FragmentManager，并开启事务
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // 替换容器中的Fragment
        ft.replace(R.id.container,fragment);
        // 提交事务
        ft.commit();
    }
}